home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / TCL1 / CEDITOR_ / CEDITORT.C < prev    next >
Text File  |  1990-06-10  |  6KB  |  239 lines

  1. /******************************************************************************
  2.  CEditorTask.c
  3.  
  4.                             The EditorTask Class
  5.  
  6.     Implements undo for cut, copy, paste, clear type and delete commands for
  7.     the CEditorPane class.
  8.     
  9.      Written by Johan A. Goossens
  10.      CompuServe: 74010,2576
  11.          
  12.     SUPERCLASS = CTask.c
  13.  ******************************************************************************/
  14.  
  15. #include            <Commands.h>
  16. #include            <CClipboard.h>
  17. #include            "CEditorTask.h"
  18.  
  19. extern CClipboard    *gClipboard;
  20.  
  21.  
  22. /******************************************************************************
  23.  IEditorTask
  24.         Initialize an EditorTask object.
  25.         FirstTaskIndex gives the index into the STR# 130 (STRtaskNames)
  26.         resource where the undo strings reside. Strings for cut, copy, paste,
  27.         clear,type and delete commands are required (and in that order).
  28.         firstTaskIndex gives the index for cut.
  29.  ******************************************************************************/
  30.  
  31. void CEditorTask::IEditorTask(CEditorPane *theEditorPane,
  32.                                     int firstNameIndex,
  33.                                     long theCommand)
  34. {
  35.     int    index;
  36.  
  37.     if (theCommand > cmdMyClear)
  38.         index = firstNameIndex + theCommand - cmdMyTyping + 4;
  39.     else
  40.         index = firstNameIndex + theCommand - cmdMyCut;
  41.  
  42.     inherited::ITask(index);
  43.  
  44.     itsCommand = theCommand;
  45.     itsEditorPane = theEditorPane;
  46.     hasData = FALSE;
  47. }
  48.  
  49.  
  50. /******************************************************************************
  51.  Dispose {OVERRIDE}
  52.  ******************************************************************************/
  53.  
  54. void    CEditorTask::Dispose()
  55. {
  56.     if (hasData)
  57.         DisposHandle(itsBuffer);
  58.     inherited::Dispose();
  59. }
  60.  
  61.  
  62. /******************************************************************************
  63.  Do {OVERRIDE}
  64.          Perform the task.
  65.  ******************************************************************************/
  66.  
  67. void    CEditorTask::Do()
  68. {
  69.     long    dummy;
  70.  
  71.     switch (itsCommand)
  72.     {
  73.         case cmdMyCut:
  74.         case cmdMyCopy:
  75.             itsEditorPane->GetSelection(&startSel, &end1Sel);
  76.             hasData = gClipboard->GetData('TEXT', &itsBuffer);
  77.             itsEditorPane->DoCommand(itsCommand);
  78.             break;
  79.  
  80.         case cmdMyPaste:
  81.             itsEditorPane->GetSelection(&startSel, &end1Sel);
  82.             hasData = itsEditorPane->GetSelectionHandle(&itsBuffer);
  83.             itsEditorPane->DoCommand(itsCommand);
  84.             itsEditorPane->GetSelection(&dummy, &end2Sel);
  85.             break;
  86.  
  87.         case cmdMyClear:
  88.             itsEditorPane->GetSelection(&startSel, &end1Sel);
  89.             hasData = itsEditorPane->GetSelectionHandle(&itsBuffer);
  90.             itsEditorPane->DoCommand(itsCommand);
  91.             break;
  92.  
  93.         case cmdMyTyping:
  94.             itsEditorPane->GetSelection(&startSel, &end1Sel);
  95.             hasData = itsEditorPane->GetSelectionHandle(&itsBuffer);
  96.             end2Sel = startSel + 1;
  97.             break;
  98.  
  99.         case cmdMyDelete:
  100.             itsEditorPane->GetSelection(&startSel, &end1Sel);
  101.             if (startSel == end1Sel)
  102.             {
  103.                 itsEditorPane->SetSelection(startSel - 1, startSel);
  104.                 hasData = itsEditorPane->GetSelectionHandle(&itsBuffer);
  105.             }
  106.             else
  107.                 hasData = itsEditorPane->GetSelectionHandle(&itsBuffer);
  108.             break;
  109.     }
  110. }
  111.  
  112.  
  113. /******************************************************************************
  114.  Type
  115.      Adjust the counter in a typing task.
  116.  ******************************************************************************/
  117.  
  118. void    CEditorTask::Typing(int offset)
  119. {
  120.     end2Sel += offset;
  121. }
  122.  
  123.  
  124. /******************************************************************************
  125.  Undo {OVERRIDE}
  126.      Undo the command.
  127.  ******************************************************************************/
  128.  
  129. void    CEditorTask::Undo()
  130. {
  131.     Boolean                oldHasData;
  132.     Handle                oldItsBuffer;
  133.  
  134.     switch (itsCommand)
  135.     {
  136.         case cmdMyCut:    
  137.             itsEditorPane->SetSelection(startSel, startSel);
  138.             itsEditorPane->DoCommand(cmdMyPaste);
  139.             itsEditorPane->SetSelection(startSel, end1Sel);
  140.             if (hasData)
  141.                 gClipboard->PutData('TEXT', itsBuffer);
  142.             break;
  143.  
  144.         case cmdMyCopy:    
  145.             itsEditorPane->SetSelection(startSel, end1Sel);
  146.             if (hasData)
  147.                 gClipboard->PutData('TEXT', itsBuffer);
  148.             break;
  149.  
  150.         case cmdMyPaste:
  151.             itsEditorPane->SetSelection(startSel, end2Sel);
  152.             if (hasData)
  153.                 itsEditorPane->SetSelectionHandle(itsBuffer);
  154.             else
  155.                 itsEditorPane->DoCommand(cmdMyClear);
  156.             break;
  157.  
  158.         case cmdMyClear:
  159.             itsEditorPane->SetSelection(startSel, startSel);
  160.             if (hasData)
  161.                 itsEditorPane->SetSelectionHandle(itsBuffer);
  162.             break;
  163.  
  164.         case cmdMyTyping:
  165.             itsEditorPane->ResetTyping();
  166.             itsEditorPane->SetSelection(startSel, end2Sel);
  167.             oldHasData = hasData;
  168.             oldItsBuffer = itsBuffer;
  169.             hasData = itsEditorPane->GetSelectionHandle(&itsBuffer);
  170.             if (oldHasData)
  171.             {
  172.                 itsEditorPane->SetSelectionHandle(oldItsBuffer);
  173.                 DisposHandle(oldItsBuffer);
  174.             }
  175.             else
  176.                 itsEditorPane->DoCommand(cmdMyClear);
  177.             break;
  178.  
  179.         case cmdMyDelete:
  180.             if (startSel == end1Sel)
  181.             {
  182.                 itsEditorPane->SetSelection(startSel - 1, startSel - 1);
  183.                 itsEditorPane->SetSelectionHandle(itsBuffer);
  184.                 itsEditorPane->SetSelection(startSel, startSel);
  185.             }
  186.             else
  187.             {
  188.                 itsEditorPane->SetSelection(startSel, startSel);
  189.                 itsEditorPane->SetSelectionHandle(itsBuffer);
  190.             }
  191.             break;
  192.     }
  193. }
  194.  
  195.  
  196. /******************************************************************************
  197.  Redo {OVERRIDE}
  198.          Redo the command.
  199.  ******************************************************************************/
  200.  
  201. void    CEditorTask::Redo()
  202. {
  203.     Boolean                oldHasData;
  204.     Handle                oldItsBuffer;
  205.  
  206.     switch (itsCommand)
  207.     {
  208.         case cmdMyCut:    
  209.         case cmdMyCopy:    
  210.         case cmdMyPaste:
  211.         case cmdMyClear:
  212.             itsEditorPane->SetSelection(startSel, end1Sel);
  213.             itsEditorPane->DoCommand(itsCommand);
  214.             break;
  215.  
  216.         case cmdMyTyping:
  217.             itsEditorPane->SetSelection(startSel, end1Sel);
  218.             oldHasData = hasData;
  219.             oldItsBuffer = itsBuffer;
  220.             hasData = itsEditorPane->GetSelectionHandle(&itsBuffer);
  221.             if (oldHasData)
  222.             {
  223.                 itsEditorPane->SetSelectionHandle(oldItsBuffer);
  224.                 itsEditorPane->SetSelection(end2Sel, end2Sel);
  225.                 DisposHandle(oldItsBuffer);
  226.             }
  227.             else
  228.                 itsEditorPane->DoCommand(cmdMyClear);
  229.             break;
  230.  
  231.         case cmdMyDelete:
  232.             if (startSel == end1Sel)
  233.                 itsEditorPane->SetSelection(startSel - 1, startSel);
  234.             else
  235.                 itsEditorPane->SetSelection(startSel, end1Sel);
  236.             itsEditorPane->DoCommand(cmdMyClear);            
  237.             break;
  238.     }
  239. }